package maze.xml;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import maze.levels.Animal;
import maze.levels.Knowledge;
import maze.levels.Level;
import maze.levels.Question;

/**
 *
 * @author loki
 */
public class XMLParser {
    public static String[] types = {"kat" , "paard", "hamster", "cavia", "konijn", "hond"};
    private Document doc;
    private URL baseURL;
    
    public XMLParser(URL baseURL, String xml) throws Exception {
        this.baseURL = baseURL;
        
	DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        
        this.doc = dBuilder.parse(new URL(baseURL, xml).toString());
    }
    
    /*public List<KnowItAll> getKnowItAlls(int type) {
        List<KnowItAll> knowItAlls = new ArrayList();
        
        doc.getDocumentElement().normalize();
        
        Element typeElement = (Element)doc.getElementsByTagName(types[type]).item(0);
        NodeList knowItList = typeElement.getElementsByTagName("weetje");
        
        for(int x = 0; x < knowItList.getLength(); x++) {
            String text = knowItList.item(x).getTextContent();
            
            knowItAlls.add(new KnowItAll(text));
        }
        
        
        return knowItAlls;
    }*/
    
    /**
     * parse the xml file and return a List of all levels of the given type 
     */
    public List<Level> getLevels(int type) throws Exception {        
        List<Level> levels = new ArrayList();
        
        doc.getDocumentElement().normalize();
        
        Element typeElement = (Element)doc.getElementsByTagName(types[type]).item(0);
        
        NodeList levelList = typeElement.getElementsByTagName("level");
        NodeList petList = typeElement.getElementsByTagName("pet");        
        
        //Retrieve data of playable character.
        Element petElement = (Element)petList.item(0);
        String img = petElement.getElementsByTagName("img").item(0).getTextContent();
        String description = petElement.getElementsByTagName("description").item(0).getTextContent();
        String audio = petElement.getElementsByTagName("audio").item(0).getTextContent();
        
        URL imageURL = new URL(baseURL, "ress/" + img);
        URL audioURL = new URL(baseURL, "ress/" + audio);
        
        Animal animal = new Animal(imageURL, description, audioURL);
        
        for(int x = 0; x < levelList.getLength(); x++) {
            Element levelElement   = (Element)levelList.item(x);
            NodeList questionList  = levelElement.getElementsByTagName("question");
            NodeList knowledgeList = levelElement.getElementsByTagName("weetje"); 
            
            String backmusic = levelElement.getElementsByTagName("audio").item(0).getTextContent();
            URL backmusicURL = new URL(baseURL, "ress/" + backmusic);
            
            //Retrieve all questions
            List<Question> questions = new ArrayList();
            for(int y = 0; y < questionList.getLength(); y++) {
                Element questionElement = (Element)questionList.item(y);
                img = questionElement.getElementsByTagName("img").item(0).getTextContent();
                description = questionElement.getElementsByTagName("description").item(0).getTextContent();
                
                imageURL = new URL(baseURL, "ress/" + img);
                Question question = new Question(imageURL, description);
                
                NodeList answerList = questionElement.getElementsByTagName("answer");
                for(int z = 0; z < answerList.getLength(); z++) {
                    question.addAnswer(answerList.item(z).getTextContent());
                }
                
                questions.add(question);
            }
            
            //Retrieve all knowItAlls
            List<Knowledge> knowledge = new ArrayList();
            for(int y = 0; y < knowledgeList.getLength(); y++) {
                Element knowItElement = (Element)knowledgeList.item(y);
                img = knowItElement.getElementsByTagName("img").item(0).getTextContent();
                description = knowItElement.getElementsByTagName("description").item(0).getTextContent();
                
                imageURL = new URL(baseURL, "ress/" + img);
                
                knowledge.add(new Knowledge(imageURL, description));
            }
            levels.add(new Level(animal, questions, knowledge, backmusicURL));
        }
        
        return levels;
    }
}
